package cn.mymall.web.controller;

import cn.mymall.support.ResultCode;
import cn.mymall.util.UCloudFileUploadUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
public class UploadApiController {
    @RequestMapping("/api/upload/image")
    public ResultCode uploadImage(@RequestParam(value = "file", required = false) MultipartFile file, String fileDir, Integer sortNum){
        try {
            if(StringUtils.isBlank(fileDir)){
                fileDir = "movie";
            }
            File newfile = new File("/tmp/1.png");
            FileUtils.copyInputStreamToFile(file.getInputStream(), newfile);
            ResultCode resultCode = UCloudFileUploadUtil.uploadFile(newfile,fileDir);
            return resultCode;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.getFailure("上传文件异常");
        }
    }
}
